<?php

require dirname(__FILE__) . '/../twl-php/autoload.php';

try {

    $dirfrom = Consolium::getArgumentValue('--dirfrom');
    $maxsize = Consolium::getArgumentValue('--maxsize');

} catch (ConsoleArgumentException $e) {
    print "
Usage: " . Consolium::getProgramName() . " --dirfrom <dir>  --maxsize <size_px>

";
}


try {

    $dirfrom = Dir::addEndslash($dirfrom);
    $dirto = $dirfrom . "converted$maxsize/";

    Dir::checkExistsAndReadable($dirfrom);
    Dir::make($dirto);

    foreach (Dir::getFiles($dirfrom) as $sourceFile) {

        print basename($sourceFile) . ' - ';

        if (!Image::isSupportedFile($sourceFile)) {
            print "unknown image type\n";
            continue;
        }

        $destinationFile = $dirto . basename($sourceFile);

        if (file_exists($destinationFile)) {
            print "already converted\n";
            continue;
        }

        $oImage = Image::load($sourceFile);

        print $oImage->getWidth() . 'x' . $oImage->getHeigth() . ', ' . File::getHumanSize($sourceFile);

        $oImage->resizeIscribe($maxsize);
        $oImage->saveAs($destinationFile);

        print ' ==> ' . $oImage->getWidth() . 'x' . $oImage->getHeigth() . ', ' . File::getHumanSize($destinationFile);
        print "\n";
    }

} catch (Exception $e) {
    print "### " . $e->getMessage() . "\n";
}

?>